/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kompoaufgabe3;

import java.util.Hashtable;
import javax.naming.*;

/**
 *
 * @author Steffen
 */
public class KompoAufgabe3 {

    Hashtable<String, String> env;
    Context context;

    KompoAufgabe3() {
        init();
        menu();
    }

    public void init() {
        env = new Hashtable<String, String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY,
                "com.sun.jndi.fscontext.RefFSContextFactory");
        env.put(Context.PROVIDER_URL, "file:///Test");

    }

    public void menu() {
        try {
            context = new InitialContext(env);


            int eingabe = -1;
            while (eingabe != 0) {
                System.out.println("Was wollen Sie machen?\n"
                        + "(0) Programm beenden\n"
                        + "(1) Zeige Dateien\n"
                        + "(2) Zeige Verzeichnisse\n"
                        + "(3) Wechsle Verzeichnis\n"
                        + "(4) Verzeichnis hoeher\n");
                eingabe = kompoaufgabe2.Eingabe.leseInt();
                switch (eingabe) {
                    case 0:
                        context.close();
                        System.exit(0);
                        break;

                    case 1:
                        showData();

                        break;

                    case 2:
                        showDirectory();
                        break;

                    case 3:
                        changeDirectory();
                        break;

                    case 4:
                        getUp();
                        break;


                }
            }
        } catch (NamingException ex) {
            System.out.println("Reason:" + ex.getExplanation());
        
        }
    }

    public void showData() throws NamingException {

        NamingEnumeration ne = context.listBindings("");
        int i = 1;
        while (ne.hasMore()) {
            Binding b = (Binding) ne.next();
            //System.out.println(b.getClassName());
            if (b.getClassName().equals("java.io.File")) {
                System.out.println(i + " : " + b.getName());
                i++;
            }
        }

    }

    public void showDirectory() throws NamingException {


        NamingEnumeration ne = context.listBindings("");
        int i = 1;
        while (ne.hasMore()) {
            Binding b = (Binding) ne.next();
            //System.out.println(b.getClassName());
            if (b.getClassName().equals("com.sun.jndi.fscontext.RefFSContext")) {
                System.out.println(i + " : " + b.getName());
                i++;
            }
        }


    }

    public void changeDirectory() throws NamingException {
        System.out.println("Verzeichnis Nummer?");
        int eingabeI = kompoaufgabe2.Eingabe.leseInt();

        NamingEnumeration ne = context.listBindings("");
        int i = 1;
        while (ne.hasMore()) {
            Binding b = (Binding) ne.next();
            //System.out.println(b.getClassName())
            if (b.getClassName().equals("com.sun.jndi.fscontext.RefFSContext")) {
                if (i == eingabeI) {

                    context = (Context) context.lookup(b.getName());
                    return;
                }
                else
                    i++;
            }
            


        }
        System.out.println("Ordner nicht gefunden!");


    }

    public void getUp() throws NamingException {

        context = (Context) context.lookup("..");

    }

    public static void main(String[] args) {
        KompoAufgabe3 a3 = new KompoAufgabe3();
    }
}
